package com.yujq.service;

import com.yujq.Enum.AccountTypeEnum;
import com.yujq.Enum.FailReasonEnum;
import com.yujq.Enum.TransTypeEnum;
import com.yujq.bean.accountBank;
import com.yujq.bean.transFlow;
import com.yujq.dao.accountBankMapper;
import com.yujq.dao.transFlowMapper;
import com.yujq.utils.insertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class accountServiceImpl implements accountService {

    private accountBankMapper bankMapper;

    private transFlowMapper flowMapper;
    @Autowired
    public void setBankMapper(accountBankMapper bankMapper) {
        this.bankMapper = bankMapper;
    }
    @Autowired
    public void setFlowMapper(transFlowMapper flowMapper) {
        this.flowMapper = flowMapper;
    }

    //第一题：：：转账方法
    public void transferAccounts(String accountA, String accountB, int money) {

        //查询两个账户
        accountBank abankA = select(accountA);
        accountBank abankB = select(accountB);

        //转出方余额不够
        if (abankA.getBalance().intValue() < money) {
            System.err.println( FailReasonEnum.ENOUGH_REA.getMessage());
            //插入一条失败记录
            flowMapper.insert(insertUtil.inserttransFlow(accountA,accountB,money, FailReasonEnum.ENOUGH_REA.getMessage(), TransTypeEnum.FALSE.getCode()));
            return;
        }

        //账户状态非正常
        if(!(abankA.getAccountType().equals(AccountTypeEnum.NORM_VALUE.getCode())) || !(abankB.getAccountType().equals(AccountTypeEnum.NORM_VALUE.getCode()))){
            System.err.println(FailReasonEnum.NONOL_REA.getMessage());
            //插入一条失败记录
            flowMapper.insert(insertUtil.inserttransFlow(accountA,accountB,money,FailReasonEnum.NONOL_REA.getMessage(),TransTypeEnum.FALSE.getCode()));
            return;
        }

        //转账成功  出账
        abankA.setBalance(new BigDecimal(abankA.getBalance().intValue()-money));
        abankA.setAccountNumber(accountA);
        System.out.println(abankA.getAccountNumber());
        bankMapper.updateAccountBank(abankA);
        //测试！！！！
        try {
            int i = 1/0;
        }catch (Exception e){
            //插入失败记录
            flowMapper.insert(insertUtil.inserttransFlow(accountA,accountB,money,FailReasonEnum.CLIENT_REA.getMessage(),TransTypeEnum.FALSE.getCode()));
            return;
        }
        //入账
        abankB.setBalance(new BigDecimal(abankB.getBalance().intValue()+money));
        abankB.setAccountNumber(accountB);
        bankMapper.updateAccountBank(abankB);
        //插入一条成功记录
        flowMapper.insert(insertUtil.inserttransFlow(accountA,accountB,money,FailReasonEnum.SUCCESS_REA.getMessage(),TransTypeEnum.SUCCESS.getCode()));
        System.out.println(FailReasonEnum.SUCCESS_REA.getMessage());
    }

    //第二题：：：遍历成功账单
    public void listFlow(){
        List<transFlow> transFlows = flowMapper.selectByFlowAccount();
        System.out.println("按以下顺序排列：流水号"+"  "+"出账账户"+"  "+"入账账户"+"  "
        +"交易日期"+"  "+"交易金额");
        for (transFlow t: transFlows
             ) {
            System.out.print(t.getFlowAccount()+"  ");
            System.out.print(t.getOutAccount()+"  ");
            System.out.print(t.getInAccount()+"  ");
            System.out.print(t.getTransTime()+"  ");
            System.out.println(t.getAmount());
        }
    }

    //查询用户是否存在
    public accountBank select(String account) {
        return bankMapper.selectByAccountNumber(account);
    }


}
